<?php
class Page_get_m extends MY_Model{
	
	private $_table_lang = 'page_article_lang';
	private $select = 'p.id, p.created, p.create_by, p.parent_id, p._class, p.ordering, l.title, l.slug, l.keywords, l.description,l.uri';
	private $select_full = 'p.id, p.created, p.create_by, p.parent_id, p._class, l.title, l.slug, l.keywords, l.description, l.bodyhtml,l.uri';
	
	
	function __construct(){
		parent::__construct();
		$this->_table = 'page_article';
	}
	
	public function get_all(){
		$this->_table = 'page_article p';
		$this->_table_lang = 'page_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->result();
		
	}
		
	public function get( $slug ){
		$result = new stdClass();
		$this->_table .= ' p';
		$this->_table_lang .= ' l';
		$this->db->select($this->select_full);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('l.slug',$slug);
		$this->db->where('l.lang',$this->_lang);
		$article = $this->db->get($this->_table)->row();
		$result->article = $article ;
		$result->list_orther = ( $article->parent_id > 0 ) ? $this->list_orther($article -> id,$article -> parent_id) : NULL;
		return $result ;
	}
	
	function list_orther( $id, $parent_id ){
		$this->_table = 'page_article p';
		$this->_table_lang = 'page_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('p.parent_id',$parent_id);
		$this->db->where('l.lang',$this->_lang);
		$this->db->order_by('ordering');
		$this->db->where_not_in('p.id', $id);
		return $this->db->get($this->_table)->result();
	}
	
	
	
	//list de quy
	public function page_list($parent_id = 0, $sym = ''){
		$this->_table = 'page_article p';
		$this->_table_lang = 'page_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('p.parent_id',$parent_id);
		$this->db->where('l.lang',$this->_lang);
		$this->db->order_by('ordering');
		
		$result = $this->db->get($this->_table)->result();
		
		foreach($result as $item){
			$item->frefix = $sym;
			$item->child = $this->page_list($item->id,$sym.'►');
		}
		//print_r($result);exit;
		return $result;
	}
	
	public function delete($id){
		$this->_table = 'page_article';
		parent::delete($id);
		//update cho child co parent_id
		$this->db->where('parent_id',$id);//old
		$this->db->update($this->_table,array('parent_id'=>$parent_id));
		
		$this->_table = 'page_article_lang';
		parent::delete($id);
		
	}
	
	function ordering( $id, $model ){
		$this->_table = 'page_article';
		return parent::update($id,$model);
	}
}